//
//  AppCenter.swift
//  GHSwiftProject
//
//  Created by Guanghui Liao on 4/5/18.
//  Copyright © 2018 liaoworking. All rights reserved.
//

import UIKit

class AppCenter: NSObject {
    
    
    /// 用户模型
    var userModel:  GHUserModel{
        set{
            saveUserModel(model: newValue)
        }
        get {
            if let model = loadUserModel() {
                return model
            }
            return GHUserModel()
        }
    }
    
    
    static let shared = AppCenter()
    private override init() {
        super.init()
        //do something here
    }
    
    
    /// 保存用户模型
    ///
    /// - Parameter model: 模型
    public func saveUserModel(model:GHUserModel) {
        if NSKeyedArchiver.archiveRootObject(model, toFile: Factory.getDocumentsPath(withFile: "UserInfo")) {
            print("归档成功")
        }else{
            print("归档失败")
        }
    }
    
    /// 读取本地用户模型
    ///
    /// - Returns: 本地保存的用户模型
   public func loadUserModel() -> GHUserModel? {
        return NSKeyedUnarchiver.unarchiveObject(withFile: Factory.getDocumentsPath(withFile: "UserInfo")) as? GHUserModel
    }
    
    
    /// 删除本地保存的用户模型
   public func removeUserModel() {
        Factory.removeMyDocuments(with: "UserInfo")
    }
    
}
